<?php

namespace app\aiapi\controller;

use app\aiapi\service\AesUtil;
use app\aiapi\service\Wxpayv3;
use think\facade\Log;
use think\facade\Request;

class Payment
{

    protected  $url;
    protected  $appid;
    protected  $mchid;
    protected  $notify_url;
    public function __construct()
    {
        //APP支付url =
        $this -> url = "https://api.mch.weixin.qq.com/v3/pay/transactions/app";
        //小程序支付url
       // $this -> url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
        //native
        //$this -> url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
        $this -> appid = "";    //appid
        $this -> mchid = "";    //appid
        $this -> notify_url = "";    //appid
    }
    /*TODO: 1、根据商品id查询商品相关描述
     *      2、根据订单id查询订单信息（订单号、价格）
     *      3、支付方式不一样 url 和 appid是变化的。
     * */
    public function payment()
    {
        //封装发起支付参数
        $param = [
            "mchid" => $this -> mchid,    //商户号
            "out_trade_no" => "APP1217752501201407033233368018",  //订单号
            "appid" => $this -> appid,    //应用id
            "description" =>"Image形象店-深圳腾大-QQ公仔",  //商品描述
            "notify_url" =>$this -> notify_url,   //回调地址
            "amount" => [
                    "total" => 1,
                    "currency" => "CNY",
            ],
            "attach" =>[
                    //自定义数据  数组格式
            ],
        ];
        $paySerivce = new Wxpayv3();
        $paySerivce -> execute($this -> url,$param);

    }





    public function notify()
    {
        $weixinResu = Request::param();
        Log::info($weixinResu);
/*        {
            "id": "EV-2018022511223320873",
    "create_time": "2015-05-20T13:29:35+08:00",
    "resource_type": "encrypt-resource",
    "event_type": "TRANSACTION.SUCCESS",
    "summary": "支付成功",
    "resource": {
            "original_type": "transaction",
        "algorithm": "AEAD_AES_256_GCM",
        "ciphertext": "",
        "associated_data": "",
        "nonce": ""
    }
}*/
        $aesUtil =  new AesUtil("32位的商户序列号");
        $result = $aesUtil -> decryptToString("associated_data","nonce","ciphertext");
        /*
         *界面结果：
          {
    "transaction_id":"1217752501201407033233368018",
    "amount":{
        "payer_total":100,
        "total":100,
        "currency":"CNY",
        "payer_currency":"CNY"
    },
    "mchid":"1230000109",
    "trade_state":"SUCCESS",
    "bank_type":"CMC",
    "promotion_detail":[
        {
            "amount":100,
            "wechatpay_contribute":0,
            "coupon_id":"109519",
            "scope":"GLOBAL",
            "merchant_contribute":0,
            "name":"单品惠-6",
            "other_contribute":0,
            "currency":"CNY",
            "stock_id":"931386",
            "goods_detail":[
                {
                    "goods_remark":"商品备注信息",
                    "quantity":1,
                    "discount_amount":1,
                    "goods_id":"M1006",
                    "unit_price":100
                },
                {
                    "goods_remark":"商品备注信息",
                    "quantity":1,
                    "discount_amount":1,
                    "goods_id":"M1006",
                    "unit_price":100
                }
            ]
        },
        {
            "amount":100,
            "wechatpay_contribute":0,
            "coupon_id":"109519",
            "scope":"GLOBAL",
            "merchant_contribute":0,
            "name":"单品惠-6",
            "other_contribute":0,
            "currency":"CNY",
            "stock_id":"931386",
            "goods_detail":[
                {
                    "goods_remark":"商品备注信息",
                    "quantity":1,
                    "discount_amount":1,
                    "goods_id":"M1006",
                    "unit_price":100
                },
                {
                    "goods_remark":"商品备注信息",
                    "quantity":1,
                    "discount_amount":1,
                    "goods_id":"M1006",
                    "unit_price":100
                }
            ]
        }
    ],
    "success_time":"2018-06-08T10:34:56+08:00",
    "payer":{
        "openid":"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"
    },
    "out_trade_no":"1217752501201407033233368018",
    "appid":"wxd678efh567hg6787",
    "trade_state_desc":"支付成功",
    "trade_type":"MICROPAY",
    "attach":"自定义数据",
    "scene_info":{
        "device_id":"013467007045764"
    }
}
         *
         *
         *
         *
         *
         * */
    }
}